<template>
  <div>
    <FormPanel ref="formpanel" :list="formList" labelWidth="100" :cols="3" :isSubmitBtn="false" @formChange="formchangeFun" />
    <FilterTable
      ref="table"
      height="auto"
      columnsRef="partPartOutboundPartBreakEditModal"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isPagination="true"
      selectionType="single"
    ></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <AjaxButton :click="closeDrawer.bind(this, 2)" type="primary" size="small">保 存</AjaxButton>
    </div>
  </div>
</template>
<script>
import { queryPartItemInfos } from '@/api/part/partOutbound/partBreak';
import { debuglog } from 'util';
export default {
  name: '',
  props: ['formData'],
  data() {
    this.BaseTable = null;
    return {
      fetchApi: queryPartItemInfos, // 调用接口
      fetchParams: {
        partNo: this.formData.PART_NO,
        storageCode: this.formData.STORAGE_CODE
      }, // 调用数据
      columns: this.createTablelist(),
      selectedRows: [],
      formList: this.createFrom(),
      fromSubmitdata: {}
    };
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.formList.forEach(x => {
      if (x.fieldName === 'PRICE_TYPE' || x.fieldName === 'PRICE_RATE') {
        return;
      }
      x.initialValue = this.formData[x.fieldName];
    });
  },
  methods: {
    closeDrawer(type) {
      if (type == 1) {
        this.$emit('close', false);
      } else {
        this.$refs.formpanel.SUBMIT_FORM();
        if (!this.fromSubmitdata) {
          return this.closeDrawer(1);
        }
        this.$emit('change', this.fromSubmitdata);
      }
    },
    createTablelist() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '销售价',
          dataIndex: 'SALES_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '成本价',
          dataIndex: 'COST_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '账面库存',
          dataIndex: 'STOCK_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '可用库存',
          dataIndex: 'USEABLE_STOCK',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '索赔价',
          dataIndex: 'CLAIM_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '终端建议零售价格',
          dataIndex: 'INSURANCE_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        // {
        //   title: '最低销售限价',
        //   dataIndex: 'MIN_LIMIT_PRICE',
        //   sorter: true,
        //   filter: true
        // },
        {
          title: '销售限价',
          dataIndex: 'LIMIT_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '网点价',
          dataIndex: 'NODE_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '最新进货价',
          dataIndex: 'LATEST_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '建议销售价',
          dataIndex: 'INSTRUCT_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '最大库存',
          dataIndex: 'MAX_STOCK',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '最小库存',
          dataIndex: 'MIN_STOCK',
          sorter: true,
          filter: true,
          filterType: 'number'
        },

        {
          title: '借进数量',
          dataIndex: 'BORROW_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '借出数量',
          dataIndex: 'LEND_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '锁定量',
          dataIndex: 'LOCKED_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '单位',
          dataIndex: 'UNIT_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    // 价格类型数据字典
    pricelist() {
      return [
        {
          value: '12361001',
          text: '成本价'
        },
        {
          value: '12361002',
          text: '销售价'
        },
        {
          value: '12361003',
          text: '索赔价'
        },
        {
          value: '12361004',
          text: '网点价'
        },
        {
          value: '12361005',
          text: '最新进货价'
        },
        {
          value: '12361006',
          text: '含税成本价'
        },
        {
          value: '12361007',
          text: '终端建议零售价格'
        },
        {
          value: '12361008',
          text: '建议销售价'
        }
      ];
    },
    createFrom() {
      // 校验报损数量
      function checkQuantity(rule, value, callback) {
        if (value <= 0) {
          callback(new Error('报损数量必须大于0!'));
        }
        callback();
      }
      return [
        {
          type: 'INPUT',
          label: '仓库',
          fieldName: 'STORAGE_CODE',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'PART_NO',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'PART_NAME'
        },
        // {
        //   type: 'INPUT',
        //   label: '价格类型',
        //   fieldName: 'PRICE_TYPE',
        //   initialValue: '12361001',
        //   itemList: this.pricelist(),
        //   disabled: true
        // },
        // {
        //   type: 'INPUT_NUMBER',
        //   label: '价格系数',
        //   fieldName: 'PRICE_RATE',
        //   initialValue: 1,
        //   disabled: true
        // },
        {
          type: 'INPUT_NUMBER',
          label: '报损单价',
          fieldName: 'LOSS_PRICE',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '报损数量',
          fieldName: 'LOSS_QUANTITY',
          rules: [{ required: true, validator: checkQuantity, trigger: 'blur' }],
          change: val => {
            this.changeNum(val);
          }
        },
        {
          type: 'INPUT_NUMBER',
          label: '报损金额',
          fieldName: 'LOSS_AMOUNT',
          precision: 2,
          disabled: true
        }
      ];
    },
    // 改变数量
    changeNum(val) {
      // 单价系数
      // let priceRate = this.formList[4].initialValue;
      // 报损单价
      let lossPrice = this.formList.find(item => item.fieldName === 'LOSS_PRICE').initialValue;
      this.formList.find(item => item.fieldName === 'LOSS_AMOUNT').initialValue = val * lossPrice;
    },
    formchangeFun(val) {
      // console.log(val ,'表单变化');
      this.fromSubmitdata = val; // {...val};
    },
    // 选中
    selectChange(rows) {
      this.selectedRows = rows;
    }
  }
};
</script>
